<template>
  <div
    ref="scrollref"
    class="suggest"
    v-loading="loading"
    v-noResult="!loading && !songs.length"
  >
    <ul class="suggest-list">
      <li class="suggest-item" v-if="singer" @click="selectSinger(singer)">
        <div class="icon">
          <i class="icon-mine"></i>
        </div>
        <div class="name">
          <p class="text">{{ singer.name }}</p>
        </div>
      </li>
      <li
        class="suggest-item"
        v-for="song in songs"
        :key="song.id"
        @click="selectSong(song)"
      >
        <div class="icon">
          <i class="icon-music"></i>
        </div>
        <div class="name">
          <p class="text">{{ song.singer }}-{{ song.name }}</p>
        </div>
      </li>
      <!-- <div class="suggest-item" v-loading="true"></div> -->
    </ul>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { search } from '@/services/search'
import { processSongs } from '@/services/song'
import usePullup from './usePullup'
export default {
  name: 'suggest',
  props: {
    query: String,
    showSinger: {
      type: Boolean,
      default: true
    }
  },
  emits: ['selectSong', 'selectSinger'],
  setup (props, { emit }) {
    const singer = ref(null)
    const songs = ref([])
    const hasMore = ref(true)
    const page = ref(1)
    const loading = ref(true)
    watch(
      () => props.query,
      async (newq) => {
        if (!newq) return
        await searchFirst()
      }
    )
    async function searchFirst () {
      if (!props.query) {
        return
      }
      page.value = 1
      songs.value = []
      singer.value = null
      hasMore.value = true

      const result = await search(props.query, page.value, props.showSinger)
      songs.value = await processSongs(result.songs)
      loading.value = false
      singer.value = result.singer
      hasMore.value = result.hasMore
    }
    const { scrollref, ispullup } = usePullup(searchMore)
    async function searchMore () {
      if (!hasMore.value || !props.query) {
        return
      }
      ispullup.value = true
      page.value++
      const result = await search(props.query, page.value, props.showSinger)
      songs.value = songs.value.concat(await processSongs(result.songs))
      hasMore.value = result.hasMore
      ispullup.value = false
    }

    function selectSong (song) {
      emit('selectSong', song)
    }
    function selectSinger (singer) {
      emit('selectSinger', singer)
    }

    return {
      singer,
      songs,
      loading,
      scrollref,
      ispullup,
      selectSong,
      selectSinger
    }
  }
}
</script>

<style lang="scss" scoped>
.suggest {
  height: 100%;
  overflow: hidden;
  z-index: 10000;
  .suggest-list {
    padding: 0 30px;
    .suggest-item {
      display: flex;
      align-items: center;
      padding-bottom: 20px;
      .icon {
        flex: 0 0 30px;
        width: 30px;
        [class^='icon-'] {
          font-size: 14px;
          color: $color-text-d;
        }
      }
      .name {
        flex: 1;
        font-size: $font-size-medium;
        color: $color-text-d;
        overflow: hidden;
        .text {
          @include no-wrap();
        }
      }
    }
  }
}
</style>
